package org.cycuchy.hp.components.book {
	
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.effects.Move;
	import mx.events.FlexEvent;
	
	import org.cycuchy.hp.app.BrowserManager;
	import org.cycuchy.hp.components.GradientCanvas;
	import org.cycuchy.hp.data.IDatas;
	import org.cycuchy.hp.event.PreloadingImageEvent;

	public class PageWithBrowser_code extends NumeredPage {
		
		private var loaded:Boolean;
		
		private var _browseEnabled:Boolean;
		private var _fastBrowseEnabled:Boolean;
		
		[Bindable]
		public var cWidth:Number;
		
		[Bindable]
		public var cHeight:Number;
		
		[Bindable]
		public var gradiendEnabled:Boolean;
		
		public var browse:Button;
		public var fastBrowse:Button;
		
		[Bindable]
		public var left:Boolean;
		
		[Bindable]
		public var btnVC:Number;

		[Bindable]
		public var contVC:Number;
		
		[Bindable]
		public var hasCaption:Boolean;
		
		protected var _manager:BrowserManager;
		
//		public var tile:Tile;
		
		public var container:GradientCanvas;
		
		public var caption:Label;
		
//		public var mv:Move;
			
		public function PageWithBrowser_code() {
			super();
			loaded = false;
			addEventListener(PreloadingImageEvent.LOAD, load);
		}
		
		public function set manager(value:BrowserManager):void {
//			trace("manager", left);
			_manager = value;
			if (left) {
				_manager.left = this; 			
			} else {
				_manager.right = this;				
			}
		}
		
		public function init(o:IDatas):void {
		}
		
		
		protected function load(event:PreloadingImageEvent):void {
//			trace("PageWithBrowser::load");
			if (!loaded) {
				if (event.index % 2 == 0) {
					var i:int = index - event.index;
//					trace("PageWithBrowser::load", i);
					if (i == 1) {
						loaded = true;
						browseFunction(null, false);
					}
				}
			}
		}
		
		protected function browseFunction(event:MouseEvent, fast:Boolean):void {
			trace("PageWithBrowser_code::browseFunction");
			_manager.browseFunction(left, fast);
		} 

//		protected function fastBrowseFunction(event:MouseEvent):void {
//			_manager.fastBrowseFunction(left);
//		}
		
		public function loadContent(offset:Number):void {
			//trace("PageWithBrowser_code::loadContent()", offset);
			changeCaption();
		}
		
		public function set browseEnabled(value:Boolean):void {
			_browseEnabled = value;
		}
		
		public function set fastBrowseEnabled(value:Boolean):void {
			_fastBrowseEnabled = value;
		}

		[Bindable]
		public function get browseEnabled():Boolean {
			return _browseEnabled;
		}

		[Bindable]
		public function get fastBrowseEnabled():Boolean {
			return _fastBrowseEnabled;
		}
		
		private var widthChanged:Boolean;
		
		private function changeCaption():void {
			var c:String = getCaption();
//			caption.visible = false;
			if (c != null) {
////				trace("PageWithBrowser_code::changeCaption ", c);
//				widthChanged = true;
				caption.text = c;
//				caption.x = 0 - c.length * 10;
////				mv.xTo = width/2 - caption.width/2;
////				caption.visible = true;
			}
		}	
		
		protected function getCaption():String {
			return null;
		}
		
//		public function updateCompleteHandler(event:FlexEvent):void {
//			trace("updateCompleteHandler");
//			if (widthChanged) {
//				widthChanged = false;
//				mv.xTo = width/2 - caption.width/2;
//				caption.visible = true;
//			}
//		}
					
	}
	
}